//
//  SwitchViewController.m
//  MultiViewDemo
//
//  Created by Ngoc on 4/29/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SwitchViewController.h"
#import <QuartzCore/CoreAnimation.h>

@implementation SwitchViewController
@synthesize currentController,previousController,stackController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(id) init
{
    self = [super init];
    if (self) {
        stackController = [[NSMutableArray alloc]init];
        isComeBack = NO;
    }
    return self;   
}
-(void) switchToView:(UIViewController *)toView
{
    [self switchView:self.currentController toView:toView];
}
-(void) switchView:(UIViewController *)currentCtrl toView:(UIViewController *)nextCtrl
{
    
    @try {
        
        
        UIWindow *theWindow = [[UIApplication sharedApplication]keyWindow]; //currentCtrl.view.superview;
        //[currentCtrl.view removeFromSuperview];
        //[theWindow addSubview:nextCtrl.view];
        theWindow.rootViewController = nextCtrl;
        CATransition *animation = [CATransition animation];
        [animation setDuration:0.75f];
        [animation setType:kCATransitionPush];
        if (isComeBack) {
            [animation setSubtype:kCATransitionFromLeft];
            
        }
        else [animation setSubtype:kCATransitionFromRight];
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
        
        // important note for problem with view shift after transition view
        
        nextCtrl.view.frame = [[UIScreen mainScreen]applicationFrame];//[[UIScreen mainScreen]bounds];//
        // back
        if (isComeBack == NO) {
            [stackController addObject:currentCtrl];
            
        }
        self.currentController = nextCtrl;
        isComeBack = NO;
    }
    @catch (NSException *exception) {
        
    }
    @finally {
        
    }
    
}
-(void) goToPreviousView
{
    if (nil != self.currentController && stackController!=nil && [stackController count]>0) 
    {
        UIViewController *previousView = [[stackController lastObject]retain];
        [stackController removeLastObject];
        if (previousView) {
            
            isComeBack = YES;
            [self switchView:self.currentController toView:previousView];
        }
    }

}

+(SwitchViewController*) sharedInstance
{
    static SwitchViewController* sharedInstance;
    if(nil == sharedInstance)
    {
        sharedInstance = [[SwitchViewController alloc] init];
        
    }
    return sharedInstance;
    
}
- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

-(void) viewDidLoad
{
    //    self.subViewController = [[StartScreen alloc] initWithNibName:@"StartScreen" bundle:nil];
    //    [self.view insertSubview:subViewController.view atIndex:0];
    // [startScreenController release];
    //parentViewController = self;
    [super viewDidLoad];
}
- (void)viewDidUnload
{
    [stackController release];
    [self.previousController release];
    [self.currentController release];
    [super viewDidUnload];
    
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
